﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QxFramework.Core;

public class Chess:MonoBehaviour
{
    public Player player;
    public Vector2Int Pos;
    public int Id;
    public Vector2Int OriPos;
    public GameObject chessObj;
    public ChessState state;
    public bool Arrive;

    private void Start()
    {
    }
    public int ScanAround(int range)
    {
        int NumOfMines=0;
        for(int i = Pos.y - range; i <= Pos.y + range; i++)
        {
            if (i < 0 || i > 17) continue;
            for(int j=Pos.x-range;j<=Pos.x+range;j++ )
            {
                if (j < 0|| j > 11) continue;
                if (Map.Instance.cellDIC[new Vector2Int(j, i)].cellState != CellState.Chess &&
                    Map.Instance.cellDIC[new Vector2Int(j, i)].cellState != CellState.Null)
                    NumOfMines++;
            }
        }
        return NumOfMines;
    }

    public void MoveTo(Vector2Int TargetPos, float MoveTime = 0.5f)
    {
        Map.Instance.cellDIC[Pos].cellState = CellState.Null;
        Map.Instance.cellDIC[TargetPos].cellState = CellState.Chess;
        Vector2 WorldPos = Map.Instance.GetWorldPos(Pos);
        Vector2 WorldTargetPos = Map.Instance.GetWorldPos(TargetPos);
        //Debug.Log(WorldPos);
        float dis = Vector2.Distance(WorldTargetPos, WorldPos);
        Vector2 speed = (WorldTargetPos - WorldPos).normalized*dis/MoveTime;
        chessObj.GetComponent<Rigidbody2D>().velocity = speed;
        Pos = TargetPos;
        UIManager.Instance.Open("MaskUI");
        Invoke("Stop", MoveTime);
    }
    public void Stop()
    {
        UIManager.Instance.Close("MaskUI");
        chessObj.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    }
    public void BeforeSetMine()
    {
        for (int i = Pos.y - 1; i <= Pos.y + 1; i++)
        {
            if (i < 0 || i > 17) continue;
            for (int j = Pos.x - 1; j <= Pos.x + 1; j++)
            {
                if (j < 0 || j > 11) continue;
                Vector2Int vector = new Vector2Int(j, i);
                if (Map.Instance.cellDIC[vector].cellState == CellState.Chess) continue;
                player.HighLightCells.Add(vector);
                Map.Instance.HightLightCell(new Vector2Int(j,i));
            }
        }
    }
    public void SetMine(Vector2Int Pos)
    {
        if (Map.Instance.cellDIC[Pos].cellState == CellState.Null)
        {
            Map.Instance.AddMine(Pos);
            if (player.Id == 1) Map.Instance.cellDIC[Pos].cellState = CellState.RMine;
            else Map.Instance.cellDIC[Pos].cellState = CellState.BMine;
        }
    }
    public void FindPath()
    {
        for(int i = 0; i <=17; i++)
        {
            for(int j = 0; j <= 11; j++)
            {
                Vector2Int vector = new Vector2Int(j, i);
                    if (Reachable(vector))
                    {
                        Debug.Log("HighLighting");
                        player.HighLightCells.Add(vector);
                        Map.Instance.HightLightCell(new Vector2Int(j, i));
                    }
            }
        }
    }
    public bool Reachable(Vector2Int TargetPos)
    {
        if (TargetPos.x >= 0 && TargetPos.x <= 11 && TargetPos.y >= 0 && TargetPos.y <= 17)
        {
            if (Vector2.Distance(TargetPos, Pos) < 2)
            {
                if (Map.Instance.cellDIC[TargetPos].cellState != CellState.Chess)
                {
                    return true;
                }
                else return false;
            }
            else
            {
                //Debug.Log("判断1" + ((Mathf.Abs(TargetPos.x - Pos.x) == Mathf.Abs(TargetPos.y - Pos.y)) || (TargetPos.x == Pos.x) || (TargetPos.y == Pos.y)));
                //Debug.Log("判断2"+ (Mathf.Abs(TargetPos.x - Pos.x) % 2 == 0));
                //Debug.Log("判断3"+((TargetPos + Pos) / 2)+ (Map.Instance.cellDIC[(TargetPos + Pos) / 2].cellState));
                //Debug.Log("判断4"+ (Map.Instance.cellDIC[TargetPos].cellState != CellState.Chess));
                if (((Mathf.Abs(TargetPos.x - Pos.x) == Mathf.Abs(TargetPos.y - Pos.y)) || (TargetPos.x == Pos.x) || (TargetPos.y == Pos.y)))
                {
                    if (Mathf.Abs(TargetPos.x - Pos.x) % 2 == 0 && Mathf.Abs(TargetPos.y - Pos.y) % 2 == 0 &&//判断是否中心对称
                        Map.Instance.cellDIC[(TargetPos + Pos) / 2].cellState == CellState.Chess &&//判断中间有没有棋子
                        Map.Instance.cellDIC[TargetPos].cellState != CellState.Chess)//判断落脚点
                    {
                        for(int i=1;i<=Mathf.Max( Mathf.Abs(TargetPos.x-Pos.x), Mathf.Abs(TargetPos.y- Pos.y)); i++)
                        {
                            Vector2Int vector = Pos + (TargetPos - Pos) / Mathf.Max(Mathf.Abs(TargetPos.x - Pos.x), Mathf.Abs(TargetPos.y - Pos.y)) * i;
                            if (vector != (TargetPos + Pos) / 2)
                            {
                                if (Map.Instance.cellDIC[vector].cellState == CellState.Chess) return false;
                            }
                        }

                        return true;
                    }
                    else return false;
                }

                else return false;
            }
        }
        else return false;
    }

    public void ChangeState()
    {
        AudioManager.Instance.PlayEffectAudio("ChangeState", 0.4f);
        if (state == ChessState.Flying)
        {
            state = ChessState.Watching;
            Reveal();
        }
        else
        {
            state = ChessState.Flying;
            UnRavel();
        }
    }
    public void Explode(Vector2Int ExplodePos)
    {
        if (player.Id == 1) TurnController.Instance.player2.Point += 75;
        else TurnController.Instance.player1.Point += 75;
        Map.Instance.RemoveMine(ExplodePos);
        MoveTo(ExplodePos,0.25f);
        Invoke("ExpMove", 0.25f);
        player.Movement = 0;
    }
    public void ExpMove()
    {
        AudioManager.Instance.PlayEffectAudio("Explode", 0.5f);
        MoveTo(OriPos, 0.25f);
    }
    public void Reveal()
    {
        AudioManager.Instance.PlayEffectAudio("Reveal", 0.5f);
        for (int i = Pos.y - 1; i <= Pos.y + 1; i++)
        {
            if (i < 0 || i > 17) continue;
            for (int j = Pos.x - 1; j <= Pos.x + 1; j++)
            {
                if (j < 0 || j > 11) continue;
                Debug.Log("ReV");
                Vector2Int vector = new Vector2Int(j, i);
                Map.Instance.cellDIC[vector].isDisplay = true;
            }
        }
        Map.Instance.Refresh();
    }
    public void UnRavel()
    {
        for (int i = Pos.y - 1; i <= Pos.y + 1; i++)
        {
            if (i < 0 || i > 17) continue;
            for (int j = Pos.x - 1; j<= Pos.x + 1; j++)
            {
                if (j < 0 || j > 11) continue;
                Vector2Int vector = new Vector2Int(j, i);
                Map.Instance.cellDIC[vector].isDisplay = false;
            }
        }
        Map.Instance.Refresh();
    }
    public void Defuse(Vector2Int vector)
    {
        if (Map.Instance.cellDIC[vector].cellState != CellState.Null)
        {
            if (Map.Instance.cellDIC[vector].cellState == CellState.OriMine)
            {
                AudioManager.Instance.PlayEffectAudio("Ding", 0.4f);
                player.Point += 50;
            }   
            if (Map.Instance.cellDIC[vector].cellState == CellState.BMine&&player.Id==1)
            {
                AudioManager.Instance.PlayEffectAudio("Ding", 0.4f);
                player.Point += 50;
            }
            if(Map.Instance.cellDIC[vector].cellState == CellState.RMine && player.Id == 2)
            {
                AudioManager.Instance.PlayEffectAudio("Ding", 0.4f);
                player.Point += 50;
            }
            Map.Instance.RemoveMine(vector);
        }
    }
}

public enum ChessState
{
    Flying,
    Watching,
}